package com.robot.command;

import iqq.im.bean.QQMsg;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.robot.bean.CYJLScore;
import com.robot.core.Context;

/**
 * @author xtippygl
 *	成语接龙
 */
public class CYJLActor extends CommandAcotr{
	public CYJLActor(Context context, QQMsg msg) {
		super(context, msg);
	}
	
	@Override
	public void execute() {		
		StringBuffer msgStr = new StringBuffer();  
		CYJLAction action = CYJLAction.getInstance(context);
		boolean isFinish = action.checkFinish();
		if(isFinish){
			context.getConfig().put("成语接龙", "关");
			msgStr.append(action.getResult());
		}else{
			//执行接龙步骤，null 为接龙成功，否则返回提示信息
			String result = action.idiomOneByOne(msg.getFrom().getQQ(),param);			
			if(result==null)
			{
				msgStr.append("@全体成员 ☆"+msg.getFrom().getNickname()+"接龙成功！【"+param+"】");
			}
			else
			{
				msgStr.append("@"+msg.getFrom().getNickname()+"："+result);
			}	
		}
		replyQQMsg(msgStr.toString());
         
	}

}

class CYJLAction {
	private static CYJLAction action;
	private List<String> idioms = new ArrayList<String>();
	private List<String> tempIdioms = new ArrayList<String>();
	private List<CYJLScore> scores = new ArrayList<CYJLScore>();
	private Integer gameTime;//ms
	private long finishTime;
	private String lastWord;
	private long lastQQ;
	
	private CYJLAction(Context context){
		this.idioms = context.getSource().getIdioms();
		Integer gametime = Integer.valueOf(context.getConfig().get("gametime"));
		this.gameTime = gametime*60*1000;
	}

	public static CYJLAction getInstance(Context context){
		if(action==null){
			action = new CYJLAction(context);
		}
		return action;		
	}
	/**
	 * @param qq
	 * @param word
	 * @return //执行接龙步骤，null 为接龙成功，否则返回提示信息
	 */
	public String idiomOneByOne(long qq, String word) {
		String result = null;
		CYJLScore score = getScore(qq);
		//不在词库中
		if(!idioms.contains(word)){
			score.setTotal(score.getTotal()+1);
			return "【"+word+"】不在在词库中，加入词库请使用#ADD 成语 四字成语";
		}
		//已经出现
		if(tempIdioms.contains(word)){
			return "【"+word+"】此前已经出现，请按【"+lastWord+"】接龙";
		}
		if(lastQQ==qq){
			return "Sorry,不能连续接龙";
		}
		if(lastWord==null||word.charAt(0)==lastWord.charAt(3)){
			lastWord = word;
			lastQQ = qq;
			score.setRight(score.getRight()+1);
			score.setTotal(score.getTotal()+1);
			
		}else{
			result = "【"+word+"】龙头没接上龙尾，请按【"+lastWord+"】接龙";
		}	
		return result;
	}
	
	private CYJLScore getScore(long qq) {
		CYJLScore score = null;
		for(CYJLScore sc:scores){
			if(sc.getQq()==qq){
				score = sc;
				break;
			}
		}
		if(score==null){
			score = new CYJLScore(qq);
			scores.add(score);
		}
		return score;
	}

	/**
	 * @return 游戏是否结束
	 */
	public boolean checkFinish() {
		long nowTime = System.currentTimeMillis();
		boolean result = false;
		if(finishTime==0){//开始
			finishTime = nowTime + gameTime;
		}else if(finishTime>nowTime){
			//游戏进行中
		}else if(finishTime<nowTime){
			finishTime = 0;//游戏时间到
			result = true;
		}
		
		return result;
	}
	public String getResult() {
		StringBuffer result = new StringBuffer("@全体成员 本次成语接龙结束，结果如下：\n");		
		Collections.sort(scores,new Comparator<CYJLScore>(){			
			public int compare(CYJLScore o1, CYJLScore o2) {
				int r = o2.getRight()-o1.getRight();
				if(r==0){
					r = o1.getTotal()-o2.getTotal();
				}
				return r;
			}		
		});
		for(int i=0;i<scores.size();i++){
			CYJLScore score = scores.get(i);
			result.append("【"+score.getQq()+"】共接龙"+score.getTotal()+"次,正确"+score.getRight()+"次;\n");
		}
		result.append("【#SET 成语接龙 开】重新开始接龙比赛。");

		tempIdioms.clear();
		scores.clear();
		lastWord = null;
		lastQQ = 0;
		return result.toString();
	}
	
	
	public void add(String idiom) {
		this.idioms.add(idiom);
	}
	
	
}
